# -*- coding: utf-8 -*-

import requests
from requests.exceptions import RequestException
from lxml import etree
from lxml.etree import ParseError
import json
import openpyxl


def one_to_page(html):
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36'
    }
    try:
        response = requests.get(html, headers=headers)
        body = response.text  # 获取网页内容
    except Exception as e:
        print('request is error!', e)

    try:
        html = etree.HTML(body, etree.HTMLParser())  # 解析HTML文本内容
        result = html.xpath('//table[contains(@class,"table-top20") or @id="otherPL"]/tbody/tr//text()')  # 获取列表数据
        # json.load()方法可以将字符串转化为python数据类型
        # result = json.loads(result)
    except Exception as e:
        print(e)

    return result


def write_file(data):  # 将数据重新组合成字典写入文件并输出
    # 创建工作簿
    wb = openpyxl.Workbook()
    # 创建工作表
    ws = wb.active
    ws.title = 'TOP20'
    ws.append(['Jan 2021', 'Jan 2020', 'Programming Language', 'Ratings', 'Change'])
    # 填充单元格
    pos = 0
    if len(data) > 0:
        for i in range(20):
            if i == 0:
                v = data[i:5]
            else:
                v = data[pos:pos + 5]
            pos += 5
            temp_list = \
                [
                    v[0],
                    v[1],
                    v[2],
                    v[3],
                    v[4]
                ]
            ws.append(temp_list)
    wb.save('../data/Programming-Language.xlsx')


def main():
    url = 'https://www.tiobe.com/tiobe-index/'
    data = one_to_page(url)
    write_file(data)


if __name__ == '__main__':
    main()
